/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package maze;

import java.util.LinkedList;
import java.awt.Point;

/**
 *
 * @author Iskandar Sarkawi
 */
public class Robot {

    private int locX, locY;

    //Keep track of its previous locations
    private LinkedList<Point> prevLocation = new LinkedList<Point>();

    public Robot(int x, int y){
        locX = x;
        locY = y;
    }

    //Getters
    public int getX(){
        return locX;
    }

    public int getY(){
        return locY;
    }

    public void moveUp(Tile[][] map){
        map[locX][locY].free();
        locY--;
        map[locX][locY].becomeOccupied();
    }

    public void moveRight(Tile[][] map){
        map[locX][locY].free();
        locX++;
        map[locX][locY].becomeOccupied();
    }

    public void moveDown(Tile[][] map){
        map[locX][locY].free();
        locY++;
        map[locX][locY].becomeOccupied();
    }

    public void moveLeft(Tile[][] map){
        map[locX][locY].free();
        locX--;
        map[locX][locY].becomeOccupied();
    }


    /*
     * Save current location into prevLocation collection
     */
    public void saveLocation(){
        Point newLoc = new Point(locX, locY);
        prevLocation.add(newLoc);
    }

    public void undo(Tile[][] map){
        Point undoLoc = prevLocation.removeLast();
        map[locX][locY].free();
        locX = (int)undoLoc.getX();
        locY = (int)undoLoc.getY();
        //System.out.println(locX + " - " + locY);
    }

    /*
     * Checks whether previous location is empty
     */
    public boolean canUndo(){
        if(prevLocation.isEmpty())
            return false;
        else
            return true;
    }

}
